{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Automatic Data Access Management \n",
    "   In this tutorial we'll explore how to use User role to request data access from domain server."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1 - Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from syft.grid.grid_client import connect\n",
    "from syft.grid.connections.http_connection import HTTPConnection\n",
    "from syft.core.node.domain.client import DomainClient\n",
    "\n",
    "import syft as sy\n",
    "import torch as th\n",
    "\n",
    "sy.VERBOSE = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2 - Connect/Authenticate with the Domain server (Using User account)\n",
    "Now, we'll be connecting with the domain server as a user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client = connect(\n",
    "    url=\"http://localhost:5000\", # Domain Address\n",
    "    credentials= {\"email\": \"user@email.com\", \"password\": \"pwd123\"}, # User Account\n",
    "    conn_type= HTTPConnection, # HTTP Connection Protocol\n",
    "    client_type=DomainClient) # Domain Client type"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 - Check the hosted/visible datasets to me"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client.store.pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4 - Send our first data access request (Without success)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if len(client.store) > 0:\n",
    "    t = client.store[0].get(\n",
    "        request_block=True,\n",
    "        timeout_secs=15,\n",
    "        request_name=\"age_data\",\n",
    "        reason=\"I want to see the age data\",\n",
    "        delete_obj=False\n",
    "    )\n",
    "    sy.logger.critical(\"Finished requesting\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5 - Send it again (Success)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if len(client.store) > 0:\n",
    "    t = client.store[0].get(\n",
    "        request_block=True,\n",
    "        timeout_secs=15,\n",
    "        request_name=\"age_data\",\n",
    "        reason=\"I want to see the age data\",\n",
    "        delete_obj=False\n",
    "    )\n",
    "    sy.logger.critical(\"Finished requesting\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
